import React, { useEffect, useState } from 'react'
import { View } from '@tarojs/components'
import BarkCard from '../../components/barkCard/index'
import Taro, { useReachBottom, usePullDownRefresh } from '@tarojs/taro'
import { useRequest } from 'ahooks'
import { getCurrentInstance } from '@tarojs/taro'

import { request } from '../../utils/request'
import styles from './index.module.less'

const getMyBark = (page) => {
  return request('get', '/app/mypostbar', { page, size: 10 })
}
const getMyrecruit = (page) => {
  return request('get', '/app/myrecruit', { page, size: 10 })
}


const MyBark: React.FC<{}> = () => {
  // @ts-ignore
  const { type } = getCurrentInstance().router.params
  const [Barks, setBarks] = useState([])
  const [total, setTotal] = useState(0)
  const [page, setPage] = useState(1)
  const bark = useRequest(getMyBark, {
    manual: true,
    onSuccess: data => {
      // @ts-ignore
      setBarks([...Barks, ...data.data])
      // @ts-ignore
      setTotal(data.total)
    }
  })
  const recruit = useRequest(getMyrecruit, {
    manual: true,
    onSuccess: data => {
      // @ts-ignore
      setBarks([...Barks, ...data.data])
      // @ts-ignore
      setTotal(data.total)
    }
  })
  useEffect(() => {
    if (type === 'bark') {
      bark.run(page)
    } else {
      recruit.run(page)
    }

  }, [page])
  usePullDownRefresh(() => {
    setTotal(0)
    setBarks([])
    setPage(1)
    if (type === 'bark') {
      bark.run(page)
    } else {
      recruit.run(page)
    }
  })
  useReachBottom(() => {
    if (total === Barks.length) {
      Taro.showToast({
        title: "已全部加载",
        icon: 'none'
      })
      return
    }
    setPage(page + 1)
  })
  return (
    <View className={styles.MyBarks}>
      <BarkCard Barks={Barks} type={String(type)}></BarkCard>
    </View>
  )
}
export default MyBark